package cn.woochen.mvvmframe.widgets

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.view.isVisible
import cn.woochen.comlib.util.StatusBarUtil
import cn.woochen.mvvmframe.R


/**
 *头部标题
 *@author woochen
 *@time 2018/11/14 16:54
 */
class TitleView(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs), View.OnClickListener {


    private val leftIcon: Int
    private val mRightIcon: Int
    private val title: CharSequence
    private val subtitle: CharSequence
    private val rightTitle: CharSequence
    var titleListner: OnTitleClickListener? = null
    private var mTitleSize: Float = 18f
    private var mSubTitleSize: Float = 15f
    private var mRightTitleSize: Float = 17f
    private var mTitleColor: Int = Color.WHITE
    private var mSubTitleColor: Int = Color.WHITE
    private var mRightTitleColor: Int = Color.WHITE
    private var showPadding = true

    interface OnTitleClickListener {
        fun leftClick()
        fun rightClick(){}
    }

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView)
        leftIcon =
            typedArray.getResourceId(R.styleable.TitleView_left_icon, 0)
        mRightIcon = typedArray.getResourceId(R.styleable.TitleView_right_icon, 0)
        title = typedArray.getText(R.styleable.TitleView_title_name)?:""
        mTitleColor = typedArray.getColor(R.styleable.TitleView_title_color, mTitleColor)
        mTitleSize = typedArray.getFloat(R.styleable.TitleView_title_size, mTitleSize)
        mSubTitleColor = typedArray.getColor(R.styleable.TitleView_subtitle_color, mSubTitleColor)
        mSubTitleSize = typedArray.getFloat(R.styleable.TitleView_subtitle_size, mSubTitleSize)
        subtitle = typedArray.getText(R.styleable.TitleView_subtitle_name)?:""
        rightTitle = typedArray.getText(R.styleable.TitleView_right_text) ?: ""
        showPadding = typedArray.getBoolean(R.styleable.TitleView_show_padding,true)
        mRightTitleColor = typedArray.getColor(R.styleable.TitleView_right_text_color, 0)
        mRightTitleSize = typedArray.getFloat(R.styleable.TitleView_right_text_size, mRightTitleSize)
        typedArray.recycle()
    }

    private val mTitle by lazy {
        findViewById<TextView>(R.id.tv_center_title)
    }
    private val mLeftIcon by lazy {
        findViewById<ImageView>(R.id.iv_left_icon)
    }

    private val mSubTitle by lazy {
        findViewById<TextView>(R.id.tv_sub_title)
    }

    private val mRightTitle by lazy {
        findViewById<TextView>(R.id.tv_right_txt)
    }

    private val mRlTitle by lazy {
        findViewById<ViewGroup>(R.id.rl_root)
    }
    private val mRootView by lazy {
        findViewById<ViewGroup>(R.id.fl_root)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        View.inflate(context, R.layout.view_title, this)
        if (showPadding) mRootView?.setPadding(0,StatusBarUtil.getStatusBarHeight(context),0,0)
        //left icon
        mLeftIcon.let{
            it.setOnClickListener(this)
            if (leftIcon != 0) {
                it.isVisible = true
                it.setImageResource(leftIcon)
            } else {
                it.isVisible = false
            }
        }
        //标题
        mTitle.run {
            setTextColor(mTitleColor)
            isVisible = title.isNotBlank()
            textSize = mTitleSize
            text = title
        }
        //副标题
        mSubTitle.apply {
            setTextColor(mSubTitleColor)
            isVisible = subtitle.isNotBlank()
            textSize = mSubTitleSize
            text = subtitle
        }
        //right icon
        val rightIcon = this.findViewById<ImageView>(R.id.iv_right_icon)
        rightIcon.setOnClickListener(this)
        if (mRightIcon != 0){
            rightIcon.isVisible = true
            rightIcon.setImageResource(mRightIcon)
        }else{
            rightIcon.isVisible = false
        }
        //right txt
        mRightTitle?.let {
            it.setOnClickListener(this)
            it.setTextColor(mRightTitleColor)
            it.isVisible = rightTitle.isNotBlank()
            it.text = rightTitle
            it.textSize = mRightTitleSize
        }
    }

    override fun onClick(v: View?) {
        if (titleListner == null) {
            when (v?.id) {
                R.id.iv_left_icon -> toBack()
                R.id.iv_right_icon -> {
                }
            }
            return
        }
        titleListner?.apply {
            when (v?.id) {
                R.id.iv_left_icon -> leftClick()
                R.id.iv_right_icon -> rightClick()
                R.id.tv_right_txt -> rightClick()
            }
        }
    }

    private fun toBack() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            (context as? Activity)?.finishAfterTransition()
        } else {
            (context as? Activity)?.finish()
        }
    }

    fun getLeftIcon() = mLeftIcon

    fun setLeftIcon(leftIcon: Int) {
        mLeftIcon?.setImageResource(leftIcon)
    }

    fun setTitleColor(colorString: String?) {
        if (colorString == null) return
        mTitle?.setTextColor(Color.parseColor(colorString))
    }

    fun setTitleBackground(colorString: String?) {
        if (colorString == null) return
        this.findViewById<ViewGroup>(R.id.fl_root).setBackgroundColor(Color.parseColor(colorString))
    }

    fun setTitle(title: String?) {
        if (title.isNullOrBlank()) return
        mTitle?.isVisible = true
        mTitle?.text = title
    }
    fun setSubTitle(title: String?) {
        if (title.isNullOrBlank()) return
        mSubTitle?.isVisible = true
        mSubTitle?.text = title
    }

    fun setTitle(titleResId: Int) {
        mTitle?.isVisible = true
        mTitle?.setText(titleResId)
    }

    fun setRightText(content: String) {
        mTitle?.isVisible = true
        mRightTitle?.text = content
    }
}